package org.xiangfei.demo.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.xiangfei.client.service.LoginService;
import org.xiangfei.codec.bean.Auth;
import org.xiangfei.demo.netty.codec.User;
/**
 * 
 * @author feesx@qq.com
 * @version 1.0
 *
 */
public class SimpleChatClientServer {
    private String routeHost = null;
    private int routePort = 0;
    //private static EventLoopGroup group = new NioEventLoopGroup();
    //private static Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class);
    private static Channel channel = null;
    public SimpleChatClientServer(String routeHost, Integer routePort) {
        this.routeHost = routeHost;
        this.routePort = routePort;
    }
    public void connection() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class);
        try {
            // 权限验证，返回userToken，全服务器唯一，调用auth服务
            // 链接成功，调用心跳服务，更新用户状态
            // 发送消息，判断是否链接成功?
            
          
               
                bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
                bootstrap.handler(new SimpleChatClientInitializer());
                channel = bootstrap.connect(routeHost, routePort).sync().channel();
                User u = new User("sd", "dsd", "dsds");
                u.setBody("dsds");
                u.setName("dssd");
                channel.writeAndFlush(u);
               // bootstrap.option(ChannelOption.SO_BACKLOG, 128);
//                XFMessage message = new XFMessage();
//                message.setCreateDate(new Date());
//                message.setMessage("hello server");
//                message.setFromUserId("1");
//                message.setToUserId("2");
//                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//                while(true){
//                  
//                    channel.writeAndFlush(message + "\r\n");
//                }
               // channel.writeAndFlush(message).sync();
                channel.closeFuture().sync();  

          
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
    
    private boolean login(Auth auth) {
        LoginService loginService = new LoginService();
        return loginService.auth(auth);
    }
    
  
    public static void main(String[] args) {
       SimpleChatClientServer client =  new SimpleChatClientServer("localhost", 8001);
     
       try {
           client.connection();
       } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }
//       XFTopic topic = client.getTopic();
//       XFMessage message = new XFMessage();
//       message.setCreateDate(new Date());
//       message.setMessage("hello server");
//       message.setFromUserId("1");
//       message.setToUserId("2");
//       topic.publish(message);
//       
    }
}
